package com.i2works.smartluolong.api.controller.user;


import com.i2works.smartluolong.api.utils.BaseModule;
import com.i2works.smartluolong.utils.base.MsgEnum;
import com.i2works.smartluolong.utils.entity.R;
import com.i2works.smartluolong.utils.entity.Result;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.web.entity.user.UserAccountDO;
import com.i2works.smartluolong.web.entity.user.UserAccountExtendDO;
import com.i2works.smartluolong.web.entity.user.UserAccountExtendQuery;
import com.i2works.smartluolong.web.entity.user.UserLoginVO;
import com.i2works.smartluolong.web.service.user.IUserAccountExtendService;
import com.i2works.smartluolong.web.service.user.IUserAccountService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 会员中心（基础资料）
 *
 * @author koal <koal@vip.qq.com>
 * @date 2017/10/19 10:40
 */

@RestController
@RequestMapping("/user")
public class UserBasicController extends BaseModule {

    private final IUserAccountExtendService userAccountExtendService;

    private final IUserAccountService userAccountService;

    @Autowired
    public UserBasicController(IUserAccountService userAccountService, IUserAccountExtendService userAccountExtendService, IUserAccountService userAccountService1) {
        this.userAccountExtendService = userAccountExtendService;
        this.userAccountService = userAccountService1;
    }


    /**
     * 修改密码方法
     */
    @PostMapping("/password/save")
    public Result password(@ModelAttribute UserLoginVO userLoginVO) {
        return updatePassword(userLoginVO);
    }

    /**
     * 用户头像编辑
     */
    @PostMapping("/avatar/save")
    public Result updateAvatar(@ModelAttribute UserAccountExtendDO userAccountExtendDO) {
        if (userAccountExtendDO.getUserId() == null) {
            return R.error(MsgEnum.ACCESS_USERID_NULL.getMsg());
        }
        if (StringUtils.isBlank(userAccountExtendDO.getAvatar())) {
            return R.error(MsgEnum.ACCESS_AVATAR_NULL.getMsg());
        }
        try {
            userAccountExtendService.updateByUserId(userAccountExtendDO);
            return R.success(MsgEnum.ACCESS_AVATAR_SUCCESS.getMsg());
        } catch (ServiceException e) {
            return R.error(MsgEnum.ACCESS_AVATAR_SUCCESS.getMsg());
        }
    }

    /**
     * 用户详情编辑
     */
    @PostMapping("/detail/save")
    public Result updateDetail(@ModelAttribute UserAccountExtendDO userAccountExtendDO) {

        if (userAccountExtendDO.getUserId() == null) {
            return R.error(MsgEnum.ACCESS_USERID_NULL.getMsg());
        }
        return updateUserDetail(userAccountExtendDO);
    }

    /**
     * 执行更新操作
     */
    private Result updateUserDetail(UserAccountExtendDO userAccountExtendDO) {
        //查询是否存在
        UserAccountExtendQuery accountExtendQuery = new UserAccountExtendQuery();
        accountExtendQuery.setUserId(userAccountExtendDO.getUserId());
        try {
            Integer queryCount = userAccountExtendService.queryCount(accountExtendQuery);
            if (queryCount > 0) {
                userAccountExtendDO.setUpdateName(getUserName());
                //更新邮箱
                if (StringUtils.isNotBlank(userAccountExtendDO.getEmail())) {
                    UserAccountDO userAccountDO = new UserAccountDO();
                    userAccountDO.setId(userAccountExtendDO.getUserId());
                    userAccountDO.setEmail(userAccountExtendDO.getEmail());
                    userAccountDO.setUpdateName(userAccountExtendDO.getUpdateName());
                    userAccountService.update(userAccountDO);
                }
                //更新扩展信息
                userAccountExtendService.updateByUserId(userAccountExtendDO);
                return R.success();
            } else {
                return R.error(MsgEnum.ACCESS_UNKNOW.getMsg());
            }
        } catch (ServiceException e) {
            return R.error(MsgEnum.SYSTEM_ERROR.getMsg());
        }
    }


}